package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.CarPackageAuditMapper;
import cn.wolfcode.car.business.query.CarPackageAuditQueryObject;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 服务项目服务impl
 *
 * @author RNIZ
 * @date 2021/11/13
 */
@Service
@Transactional
public class CarPackageAuditServiceImpl implements ICarPackageAuditService {
    
    @Autowired
    private CarPackageAuditMapper carPackageAuditMapper;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private IBpmnInfoService bpmnService;
    @Autowired
    private IServiceItemService serviceItemService;
    @Autowired
    private TaskService taskService;
    @Override
    public TablePageInfo<CarPackageAudit> query(CarPackageAuditQueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<CarPackageAudit>(carPackageAuditMapper.selectForList(qo));
    }

    @Override
    public CarPackageAudit get(Long id) {
        return carPackageAuditMapper.selectByPrimaryKey(id);
    }


    @Override
    public void save(CarPackageAudit carPackageAudit) {
        carPackageAuditMapper.insert(carPackageAudit);
    }




    @Override
    public void update(CarPackageAudit carPackageAudit) {

        carPackageAuditMapper.updateByPrimaryKey(carPackageAudit);
    }

    @Override
    public List<CarPackageAudit> list() {
        return carPackageAuditMapper.selectAll();
    }

    @Override
    public void updateByInstanceId(CarPackageAudit carPackageAudit) {
        carPackageAuditMapper.updateByPrimaryKey(carPackageAudit);
    }

    @Override
    public InputStream processImg(Long id) {
        //判断审核状态
        CarPackageAudit audit = this.get(id);
        String instanceId = audit.getInstanceId();
        List<String> list = null;
        if (CarPackageAudit.STATUS_IN_ROGRESS.equals(audit.getStatus())){
             list = runtimeService.getActiveActivityIds(instanceId);
        }else {
            list = new ArrayList<>();
        }
        BpmnInfo bpmnInfo = bpmnService.get(audit.getBpmnInfoId());
        BpmnModel bpmnModel = repositoryService.getBpmnModel(bpmnInfo.getActProcessId());
        DefaultProcessDiagramGenerator generator = new DefaultProcessDiagramGenerator();
        return generator.generateDiagram(bpmnModel,list, Collections.EMPTY_LIST,"宋体","宋体","宋体");
    }

    @Override
    public void cancelApply(Long id) {
        //进行合理化校验
        CarPackageAudit audit = this.get(id);
        if (!(CarPackageAudit.STATUS_IN_ROGRESS.equals(audit.getStatus()))){
            throw new BusinessException("不是审核中状态");
        }
        //改变服务项的状态
        serviceItemService.updateByStatus(audit.getServiceItemId(), ServiceItem.AUDITSTATUS_INIT);
        //删除流程定义
        runtimeService.deleteProcessInstance(audit.getInstanceId(),"撤销审核");
        //改变审核列表状态
        carPackageAuditMapper.updateByStatus(id,CarPackageAudit.STATUS_CANCEL);
    }

    @Override
    @Transactional
    public void audit(Long id, Integer auditStatus, String info) {
        CarPackageAudit audit = this.get(id);
        if (!(CarPackageAudit.STATUS_IN_ROGRESS.equals(audit.getStatus()))) {
            throw new BusinessException("不是审核中状态");
        }
        String userName = ShiroUtils.getUser().getUserName();
        if (CarPackageAudit.STATUS_PASS.equals(auditStatus)) {
            info = userName + "同意,审批意见" + info;
        } else {
            info = userName + "不同意,审批意见" + info;
        }
        audit.setInfo(audit.getInfo() + "<br>" + info);
        Task task = taskService.createTaskQuery().processInstanceId(audit.getInstanceId()).singleResult();
        taskService.setVariable(task.getId(), "auditStatus", auditStatus);
        taskService.addComment(task.getId(), audit.getInstanceId(), info);
        taskService.complete(task.getId());

        Task nextTask = taskService.createTaskQuery().processInstanceId(audit.getInstanceId()).singleResult();
        if (CarPackageAudit.STATUS_PASS.equals(auditStatus)) {
            if (nextTask != null) {
                //财务审核
                audit.setAuditorId(Long.parseLong(nextTask.getAssignee()));
                audit.setAuditTime(new Date());
            } else {
                //审核通过
                audit.setAuditTime(new Date());
                serviceItemService.updateByStatus(audit.getServiceItemId(), ServiceItem.AUDITSTATUS_APPROVED);
                 audit.setStatus(CarPackageAudit.STATUS_PASS);
            }

        } else {
            serviceItemService.updateByStatus(audit.getServiceItemId(), ServiceItem.AUDITSTATUS_REPLY);
             audit.setStatus(CarPackageAudit.STATUS_REJECT);
        }
        carPackageAuditMapper.updateByPrimaryKey(audit);
    }

}
